package org.example;

import org.springframework.stereotype.Service;

@Service  // 让 Spring 管理这个 Bean
public class OrderServiceImpl implements OrderService {

    @Override
    public void add(String pname, int number) {
        System.out.println("🛒 正在添加 " + number + " 个 [" + pname + "] 到购物车");
        sleep(100); // 模拟耗时
    }

    @Override
    public void delete(String pname) {
        System.out.println("🗑️  正在从购物车删除 [" + pname + "]");
        sleep(100);
    }

    @Override
    public int find(String pname) {
        int count = (int)(Math.random() * 10); // 随机返回 0~9 个
        System.out.println("🔍 购物车中 [" + pname + "] 的数量为: " + count);
        sleep(50);
        return count;
    }

    // 模拟方法执行耗时
    private void sleep(int millis) {
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}